/**
 * Created by zhaojing on 2017/10/30.
 */

/**
 * @file
 * @auth zhaojing
 * @date 2016/11/24
 */

import './style.scss';
import tpl from './tpl.html';

import Vue from 'vue';
import modal from '../../../../../grsc-base/component/Modal.vue';
import Validator from '../../../../../grsc-base/component/Validator/main';
import Qrcode from '../../../../../grsc-base/component/Qrcode.vue';
import {REG_LOGPWD} from '../../../../../grsc-base/common/constants';
import {RULE_ASSET_PASSWORD_LENGTH, RULE_ASSET_PASSWORD_PATTERN, RULE_CODE_SEND, REG_PASSPORT} from '../../../../common/contants';
import lib from '../../../../common/lib';

import service from '../../basic/service';

export default  Vue.extend({
    template: tpl,
    components: {
        modal,
        Validator,
        Qrcode
    },
    created() {
        this.getGoogleCode();
    },
    mounted() {

    },
    watch: {},
    computed: {},
    data() {
        return {
            secretcode: '',
            googleAuthenticator: '',
            isRequesting: false,
            form: {
                loginPwd: '',
                googleCode: '',
            },
            rules: {
                loginPwd: {
                    tip: '',
                    required: true,
                    requiredTip: this.$t('common.loginPassRequireTip'),
                    minlength: 6,
                    minlengthTip: this.$t('common.ruleLoginPassLength'),
                    maxlength: 20,
                    maxlengthTip: this.$t('common.ruleLoginPassLength'),
                    pattern: REG_LOGPWD,
                    patternTip: this.$t('common.ruleLoginPassPattern')
                },
                googleCode: {
                    required: true,
                    requiredTip: this.$t('common.googleCodeRequireTip'),
                    pattern: '\\d{6}',
                    patternTip: this.$t('common.IncorrectGoogleCodeTip')
                }
            }
        }
    },
    methods: {
        /**
         * 获取谷歌验证二维码URI 密钥 及邮箱信息
         */
        getGoogleCode(){
            service.googleCodeGet().done(rep=> {
                if (rep.code === window.CODE.SUCCESS) {
                    this.googleAuthenticator = rep.data.googleAuthenticator;
                    this.secretcode = rep.data.secretcode;
                }else{
                    window.error(rep.msg)
                }
            })
        },

        /**
         * 验证登录密码
         */
        checkLogPsd(){
            this.rules.loginPwd.tip = this.$t('common.ruleLoginPassLength');
        },
        checkLogPsdBlur() {
            this.rules.loginPwd.tip = '';
        },

        submit(){
            let that = this;
            if (this.rules.$checkAll() === false) {
                return;
            }
            service.googleCodeFirstcheck({},{
                'login-password': this.form.loginPwd,
                'google-code': this.form.googleCode
            }).done(rep => {
                if (rep.code === window.CODE.SUCCESS) {
                    window.success(this.$t('common.success'));
                    that.hide();
                }else if(rep.code === window.CODE.LOGIN_PWD_ORIGIN.CODE){
                    this.rules.loginPwd.$setError( this.$t('common.IncorrectLogin'));
                }else if(rep.code === window.CODE.GOOGLE_CODE.CODE){
                    this.rules.googleCode.$setError(window.CODE.GOOGLE_CODE[this.$i18n.locale]);
                }else {
                    window.error(rep.msg);
                }
            }).always(rep => {

            })
        },

        hide() {
            this.$emit('closeGoogleAuth');
        }
    }

});
